<?php

if(!defined('IN_SYS')) exit('Access Denied');

class catchtest_controller extends admincp{

	public function __construct(){
		parent::__construct();
	} 

	public function index(){
		$id = gp("id");
		$res = db::find("select * from catch where id = '$id'");
		$listurl  = $res['listurl'];
		$temp_urllist = array();
		if(preg_match("#\[([1-9]\d*)\-([1-9]\d*)\]#", $listurl,$mt1)){
			$min = $mt1[1];
			$max = $mt1[2];
			$temp_urllist = array();
			for($i=$min;$i<=$max;$i++){
				$temp_urllist[] = preg_replace("#\[([1-9]\d*)\-([1-9]\d*)\]#", $i,$listurl);
			}
			$url=preg_replace("#\[([1-9]\d*)\-([1-9]\d*)\]#", $min, $listurl);
		}else{
			$temp_urllist[] = $listurl;
			$url = $listurl;
		}
		$result=httpUnit::get($url);
		$data="";

		if($result['httpcode']==200){
			$data=$result['html'];
		}else{
			echo "无法采集此网站";
			exit;
		} 

		//判断编码
		$unicode=htmlBase::get_charset($data);
		//转换编码
		if(!empty($unicode) && $unicode!=="utf-8"){
			$data=mb_convert_encoding($data,"utf-8",$unicode);
		}
		$html2 = htmlHelper::matchcontent($data, $res['articlerule']);
		$arr_link = array();
		if(preg_match_all("#href=[\"'\s]*([^\s\"'\\\\]*)[\"'\s]#", $html2, $mt3)){
			$arr_link = $mt3[1];
		}
		if(!empty($res['norule'])){
			$norule=explode("\n",$res['norule']);
			foreach ($norule as  $k => $value) {
				$norule[$k] = trim($value);
			}
			$norule=array_filter($norule);
			foreach ($arr_link as $k => $value) {
				foreach ($norule as $x => $y) {
					if(stripos($value, $y)!==false){
						unset($arr_link[$k]);
					}
				}
			}
		}

		$title="";
		$body="";
		$res_un = array_unique($arr_link);
		$res_val = array_values($res_un);
		if(!empty($res_val[0])){
			$rand_url = rand(0,9);
			$val = $this->catchtitle($res_val[$rand_url],$res);
			$title = $val['title'];
			$body = $val['body'];
		}
		require self::tpl('catch_test');
	}

	public function catchtitle($url,$res){

		$url =  htmlHelper::fix_path($res['listurl'],$url);
		$result=httpUnit::get($url);
		$data="";

		if($result['httpcode']==200){
			$data=$result['html'];
		}else{
			echo "无法采集此文章";
			exit;
		}

		//判断编码
		$unicode=htmlBase::get_charset($data);
		//转换编码
		if(!empty($unicode) && $unicode!=="utf-8"){
			$data = mb_convert_encoding($data,"utf-8",$unicode);
		} else if (strrpos($url, 'yuanlin.com')) {
			$data = mb_convert_encoding($data,"utf-8",'gbk');
		}
		if (empty($unicode)){
			echo "无法识别编码：$url";
			exit;
		}

		//匹配文章标题
		$title = htmlHelper::matchcontent2($data, $res['titlerule']);
        var_dump($title);exit;
		$title = str_replace(array("\r\n", "\r", "\n"), "", $title);

		//匹配文章内容
		$body = htmlHelper::matchcontent($data, $res['contentrule']);

		//文章内容处理
		//处理a标签
		$filecontent = preg_replace('#<[/]?a[^>]*?>#', "", $body);
		//过滤div标签
		$filecontent = preg_replace('#<[/]?div[^>]*?>#', "", $filecontent );
		//过滤空
		$filecontent = preg_replace('#<([a-z]*?)\s[^>]*></\\1>#', "", $filecontent );

		//图片处理
		$filecontent = yumi::convert_images_patch($filecontent);

		//过滤文章内容
		$filecontent = htmlBase::tag_filter($filecontent);
		//过滤data-original
		// $filecontent = preg_replace('/data-original=\"(.*?)\"/is', '',$filecontent);
		$filecontent = str_replace("</p></p>","</p>", $filecontent );
		$filecontent = str_replace("<p><p align=\"center\">","<p align=\"center\">", $filecontent );

		$result['title'] = $title;
		$result['body'] = $body;
		return $result;

	}
}

